#include <iostream>
#include "NewtonInterpolation.h"
#include <vector>
#include <cmath>
#include <iomanip>
#include "matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
    vector<double> t = {0,0,3,3,5,5,8,8,13,13};
    vector<double> ds = {0,75,225,77,383,80,623,74,993,72};
    Hermite H(t,ds);
    Polynomial dis = H.polation();
    Polynomial sp = dis.derive();

    cout << "D : (a) when t = 10 : " << endl;
    cout << "position = " << setiosflags(ios::fixed) << setprecision(0) << dis(10) << endl;
    cout << "speed = " << setiosflags(ios::fixed) << setprecision(0) << sp(10) << endl;
    

        //以下为画图部分
    int n = 1000;    //描点画图的点数
    double a = 0 , b = 13.0;  //区间
    double gap = (b - a) / n;
    std::vector<double> x(n+1), y(n+1);

    plt::figure();          //创建一张图
        for (int j = 0; j <= n; j++)
        {
            x[j] = a + j * gap;
            y[j] = dis(x[j]);
        }

    plt::plot(x, y);
        
    plt::xlim(a, b);
    plt::title("D-distance"); // set a title

    plt::save("D-distance.png");


    plt::figure();          //创建一张图
        for (int j = 0; j <= n; j++)
        {
            x[j] = a + j * gap;
            y[j] = sp(x[j]);
        }

    plt::plot(x, y);
        
        for (int j = 0; j <= n; j++)
        {
            y[j] = 81;
        }
        plt::plot(x, y,"--");

    plt::xlim(a, b);
    plt::title("D-speed"); // set a title

    plt::save("D-speed.png");

    cout << "(b) when t = 12, speed = " << setiosflags(ios::fixed) << setprecision(0) << sp(12) << endl;
    cout << "So exceed. We can also find it in the plot." << endl;
    cout << endl;

}